package com.ruoyi.celoan.util;

import com.xmsme.national.secrets.NationalSecretsUtils;
import com.xmsme.national.secrets.RandomStringUtils;
import com.xmsme.national.secrets.SM3Digest;
import com.xmsme.national.secrets.SM4Utils;
import com.xmsme.national.secrets.util.encoders.Base64;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.crypto.codec.Hex;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class EncryptDecryptTest {
    private static final Logger log = LoggerFactory.getLogger(EncryptDecryptTest.class);
    private static final String USER_ID = "xmsme";
    public static final String UTF_8 = "UTF-8";
    private static final String privateKey = "00C53F7CB52D1320622AF326F522557DC77DD932AB57099EFE43F0F0B6E695FA26";
    private static final String publicKey = "04C94320A5F38226438DC8804CA77012B507002A056C1E3484ED3EDBCCD5D8961354F6DE35D3979051E3815C0DA396B2A2939345A2D3FD3FEAF55B15D54E5DFD2E";

    @Test
    public void test_encrypt_decrypt() throws Exception {
        String sourceData = "[{\"uniscId\": \"\",\"enterpriseName\": \"\",\"realNameCertification\": \"\",\"operatorName\": \"\",\"operatorIdentity\": \"\",\"operatorIdCardNo\": \"\",\"loaned\": \"\",\"address\": \"\",\"industry\": 0,\"province\": \"\",\"city\": \"\",\"area\": \"\",\"registeredCapital\": 0,\"businessScope\": \"\",\"operatingTimeLimitType\": 0,\"operatingTimeLimitDateBegin\": \"2019-02-10\",\"operatingTimeLimitDateEnd\": \"2019-02-10\",\"approvalDate\": \"2019-02-10\",\"settlingTime\": \"2019-02-10 10:21:15\",\"externalSystemId\": \"\"}]";
        log.info("原始数据：{}", sourceData);
        String summary = summary(sourceData);
        log.info("摘要:{}", summary);
        String sign = sign(summary);
        log.info("请求signatureData:{}", sign);
        String randomSm4Key = RandomStringUtils.getRandomString();
        log.info("随机sm4密码:{}", randomSm4Key);
        SM4Utils sm4 = new SM4Utils(randomSm4Key);
        String encryptData = sm4.encryptData_CBC(sourceData);
        log.info("请求中data:{}", encryptData);
//        String requestKey = SM2Utils.encrypt(NationalSecretsUtils.hexToByte("04C94320A5F38226438DC8804CA77012B507002A056C1E3484ED3EDBCCD5D8961354F6DE35D3979051E3815C0DA396B2A2939345A2D3FD3FEAF55B15D54E5DFD2E"), randomSm4Key.getBytes("UTF-8"));
        String requestKey = null;
        log.debug("请求中key：{}", requestKey);
//        String decryptSm4Key = SM2Utils.decrypt("00C53F7CB52D1320622AF326F522557DC77DD932AB57099EFE43F0F0B6E695FA26", requestKey);
        String decryptSm4Key = null;
        log.info("请求key解密结果:{}", decryptSm4Key);
        SM4Utils decryptSm4 = new SM4Utils(decryptSm4Key);
        String decryptData = decryptSm4.decryptData_CBC(encryptData);
        log.info("请求data解密结果:{}", decryptData);
        boolean verified = verifiedSign(decryptData, sign);
        log.info("请求验签结果:{}", Boolean.valueOf(verified));
    }

    private String summary(String msg) throws UnsupportedEncodingException {
        byte[] md = new byte[32];
        SM3Digest sm = new SM3Digest();
        sm.update(msg.getBytes("UTF-8"), 0, (msg.getBytes("UTF-8")).length);
        sm.doFinal(md, 0);
        String s = new String(Hex.encode(md));
        return s.toUpperCase();
    }

    private String sign(String summaryString) throws IllegalArgumentException, IOException {
        String privateKeys = new String(Base64.encode(NationalSecretsUtils.hexToByte("00C53F7CB52D1320622AF326F522557DC77DD932AB57099EFE43F0F0B6E695FA26")));
        /*byte[] sign = SM2Utils.sign("xmsme"
                        .getBytes("UTF-8"),
                Base64.decode(privateKeys.getBytes("UTF-8")),
                NationalSecretsUtils.hexToByte(summaryString));*/
        byte[] sign = null;
        return NationalSecretsUtils.getHexString(sign);
    }

    public boolean verifiedSign(String decryptData, String sign) throws IOException {
        String summary = summary(decryptData);
        /*boolean verified = SM2Utils.verifySign("xmsme"
                        .getBytes("UTF-8"),
                NationalSecretsUtils.hexToByte("04C94320A5F38226438DC8804CA77012B507002A056C1E3484ED3EDBCCD5D8961354F6DE35D3979051E3815C0DA396B2A2939345A2D3FD3FEAF55B15D54E5DFD2E"),
                NationalSecretsUtils.hexToByte(summary),
                NationalSecretsUtils.hexToByte(sign));*/
        boolean verified = true;
        return verified;
    }
}


/* Location:              D:\tuodayun\workspace\work\celoan-third-data-report-service-2.0.0-SNAPSHOT.jar!\BOOT-INF\classes\com\celoa\\util\EncryptDecryptTest.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */
